#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

#include "../../../../core/shader_std.h"
#include "../../../../core/shader_binding_ID.h"
#include "../../../../core/shader_color.h"


layout(location = 0) in vec4 vColor;
layout(location = 1) in vec2 vUV;
layout(location = 2) in flat float gap;
layout(location = 3) in flat float offset;
layout(location = 4) in flat uint shader_type;
layout(location = 5) in flat uint offset_texID;
layout(location = 6) in flat uint texNum;

layout(location=0) out vec4 outColor;

layout(set = 4, binding = DEF_binding_L4_TexCube)  uniform samplerCube texCubeArray[];


void main(void) {
	vec2 r = gl_PointCoord;

	switch(vDrawType>>16){
		case DEF_ObType_SamplerType_None : 
			outColor = texture(tex, vUV);
			break;
		case DEF_ObType_SamplerType_2DArray : 
			outColor = texture(texArray[nonuniformEXT(vTexID)], vUV); 
			break;
		case DEF_ObType_SamplerType_CubeArray : {
			vec3 cubeUV;
			//vec2 UV = vUV - 0.5;
			cubeUV = vec3(vUV, 0) * 2 - vec3(1);
			switch(vCubeTexID){
				case DEF_ObType_CubeTexDir_PX : {
					cubeUV = vec3(1, cubeUV.y * -1, cubeUV.x * -1);
					break;
				}
				case DEF_ObType_CubeTexDir_NX : {
					cubeUV = vec3(-1, cubeUV.y * -1, cubeUV.x);
					break;
				}
				case DEF_ObType_CubeTexDir_PY : {
					cubeUV = vec3(cubeUV.x, 1, cubeUV.y);
					break;
				}
				case DEF_ObType_CubeTexDir_NY : {
					cubeUV = vec3(cubeUV.x, -1, cubeUV.y * -1);
					break;
				}
				case DEF_ObType_CubeTexDir_PZ : {
					cubeUV = vec3(cubeUV.x, cubeUV.y * -1, 1);
					break;
				}
				case DEF_ObType_CubeTexDir_NZ : {
					cubeUV = vec3(cubeUV.x, cubeUV.y, -1);
					break;
				}
			}
			
			//cubeUV = normalize(cubeUV);
			outColor = texture(texCubeArray[nonuniformEXT(vTexID)], cubeUV); 
			outColor.rgb += outColor.rgb * outColor.a;
			outColor.a = 1;
			//outColor = vec4(1);
			return;
		}
		
	}
	//if(bool(vDrawType>>16)){
	//	outColor = texture(tex, vUV);
	//	//outColor = vec4(1,0,0,1);
	//}else{
	//	outColor = texture(texArray[nonuniformEXT(vTexID)], vUV);
	//}
	
	//outColor = textureLod(tex[nonuniformEXT(vTexID)], vec2(vUV), 0);
	float vIntensity = 0;
	

	switch(vDrawType & 0x0000ffff){
		
		case DEF_DrawType_ICO_Color:
			outColor.rgb = outColor.rgb * vColor.rgb;
			break;

		case DEF_DrawType_ICO_CircularBackground: {
			r = (gl_PointCoord - 0.5);
			float a = (dot(r, r));
			a = clamp(0.5 - a - 0.25, 0.0, 0.25) * 20;
			
			vec4 background = vec4(vColor.rgb, a);
			outColor.rgb = mix(background.rgb, outColor.rgb + vIntensity, outColor.a);
			outColor.a = background.a;
			break;
		}

		case DEF_DrawType_Col_CircularBackground : {
			r = (gl_PointCoord - 0.5);
			float a = (dot(r, r));
			a = clamp(0.5 - a - 0.25, 0.0, 0.25) * 20;

			outColor = vec4(vColor.rgb, a);
			break;
		}

		case DEF_ICO_Color_Monochrome : {
			float w = dot(vColor.rgb, vec3(0.2125,0.7154,0.0721));
			outColor.rgb = vec3(w);
			break;
		}

		case DEF_DrawType_ICO_ColorInvert:
			outColor.rgb = (vec3(1.0) - outColor.rgb) * vColor.rgb;
			break;

		case DEF_DrawType_Linear : {
			outColor.rgb = (vec3(1.0) - outColor.rgb) * vColor.rgb;
			break;
		}
		case DEF_DrawType_Depth : {
			outColor.rgb *= 0.001;
			break;
		}
		case DEF_ICO_DynamicMonochrome : {
			outColor.rgb = vColor.rgb;
			break;
		}
		

		default:
			break;
	}
}


 